package com.manlost.web.controller.tag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manlost.biz.ManlostServiceLocator;
import com.manlost.biz.baby.model.constants.BabyType;
import com.manlost.biz.tag.TagService;
import com.manlost.biz.tag.model.constants.TagType;
import com.manlost.biz.tag.model.dataobject.BabyTagAccoutRelation;
import com.manlost.biz.tag.model.dto.TagInfoDTO;
import com.manlost.biz.tag.query.ListTagIdsQuery;
import com.manlost.web.common.ManLostSession;
import com.manlost.web.common.WebUser;

/**
 * 类TagAjaxController.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-4-15 下午2:33:15
 */
@Controller
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TagAjaxController {

    private TagService          tagService         = ManlostServiceLocator.getTagService();
    private static final String RESULT_KEY         = "result";
    private static final String NEED_LOGIN         = "need_login";
    private static final String PARAMS_BLANK_ERROR = "params_error";
    private static final String BABY_TYPE_ERROR    = "baby_type_error";
    private static final String TAG_SUCCESS        = "tag_success";
    private static final String REMOVE_TAG_SUCCESS = "remove_tag_success";
    private static final String SYS_ERROR          = "sys_error";

    /**
     * 给前台baby打tag的ajax请求
     */
    @RequestMapping(value = "/tag/tagBaby.htm", method = RequestMethod.POST)
    public String tagBaby(Map model, Long babyId, String babyType) {
        WebUser webUser = ManLostSession.getCurrent().getWebUser();
        if (!webUser.isSignIn()) {
            return "login/login";
        }
        List<TagInfoDTO> tagInfos = tagService.listTagInfos();
        model.put("tagInfos", tagInfos);

        // 查询已经打好了的标签展示出来
        ListTagIdsQuery query = new ListTagIdsQuery();
        query.setBabyId(babyId);
        query.setAccountId(webUser.getId());
        query.setTagType(TagType.ACCOUNT.getValue());
        List<Long> tagIds = tagService.listTagIdsByQuery(query);

        model.put("babyId", babyId);
        model.put("babyType", babyType);
        model.put("tagIds", tagIds);
        return "tag/tagBaby";
    }

    @RequestMapping(value = "/tag/addBabyTag.htm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addBabyTag(Map model, Long tagId, Long babyId, String babyType) {
        Map<String, Object> result = new HashMap<String, Object>();

        WebUser webUser = ManLostSession.getCurrent().getWebUser();
        if (!webUser.isSignIn()) {
            result.put(RESULT_KEY, NEED_LOGIN);
            return result;
        }

        if (tagId == null || babyId == null || StringUtils.isBlank(babyType)) {
            result.put(RESULT_KEY, PARAMS_BLANK_ERROR);
            return result;
        }
        // 开始打标签，该Ajax请求只用于用户打标签
        BabyTagAccoutRelation record = new BabyTagAccoutRelation();
        BabyType type = BabyType.getEnum(babyType);
        if (type == null) {
            result.put(RESULT_KEY, BABY_TYPE_ERROR);
            return result;
        }
        record.setBabyType(type);
        record.setBabyId(babyId);
        // record.setAccountId(webUser.getId());
        record.setTagId(tagId);
        record.setTagType(TagType.ACCOUNT);
        if (!tagService.addBabyTag(record)) {
            result.put(RESULT_KEY, SYS_ERROR);
            return result;
        }
        result.put(RESULT_KEY, TAG_SUCCESS);
        return result;
    }

    @RequestMapping(value = "/tag/removeBabyTag.htm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> removeBabyTag(Map model, Long tagId, Long babyId, String babyType) {
        Map<String, Object> result = new HashMap<String, Object>();

        WebUser webUser = ManLostSession.getCurrent().getWebUser();
        if (!webUser.isSignIn()) {
            result.put(RESULT_KEY, NEED_LOGIN);
            return result;
        }

        if (tagId == null || babyId == null) {
            result.put(RESULT_KEY, PARAMS_BLANK_ERROR);
            return result;
        }
        // 开始打标签，该Ajax请求只用于用户打标签
        BabyTagAccoutRelation record = new BabyTagAccoutRelation();
        BabyType type = BabyType.getEnum(babyType);
        if (type == null) {
            result.put(RESULT_KEY, BABY_TYPE_ERROR);
            return result;
        }
        record.setBabyType(babyType);
        record.setBabyId(babyId);
        record.setTagId(tagId);
        record.setAccountId(webUser.getId());
        if (!tagService.removeBabyTag(record)) {
            result.put(RESULT_KEY, SYS_ERROR);
            return result;
        }
        result.put(RESULT_KEY, REMOVE_TAG_SUCCESS);
        return result;
    }

}
